package org.bing.engine.utility.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class StringHelper {
    public static char PROPERTY_PREFIX = '@';

    public static boolean isBlank(String str) {
        return str == null ? true : str.trim().length() == 0;
    }

    public static boolean startsWith(String s1, String s2) {
        if (s1 == s2) {
            return true;
        } else if (s1 == null || s2 == null) {
            return false;
        }
        return s1.startsWith(s2);
    }

    public static boolean equals(String s1, String s2) {
        return equals(s1, s2, false, false);
    }

    public static boolean equals(String s1, String s2, boolean trim) {
        return equals(s1, s2, trim, false);
    }

    public static boolean equals(String s1, String s2, boolean trim, boolean ignore) {
        if (s1 == s2) {
            return true;
        } else if (s1 != null && s2 != null) {
            if (trim) {
                if (ignore) {
                    return s1.trim().equalsIgnoreCase(s2.trim());
                } else {
                    return s1.trim().equals(s2.trim());
                }
            } else {
                if (ignore) {
                    return s1.equalsIgnoreCase(s2);
                } else {
                    return s1.equals(s2);
                }
            }
        } else {
            return false;
        }
    }

    public static boolean getBoolean(String s) {
        if (s == null) {
            return false;
        } else {
            return Boolean.valueOf(s).booleanValue();
        }
    }

    public static String[] simpleSplit(String src, char sc) {
        return simpleSplit(src, new char[] { sc });
    }

    public static String[] simpleSplit(String src, char[] scs) {
        if (src == null) {
            return null;
        }
        int len = src.length();
        String[] res = new String[8];
        int num = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            char c = src.charAt(i);
            boolean f = false;
            for (int n = 0, l = scs.length; n < l; n++) {
                if (c == scs[n]) {
                    f = true;
                    break;
                }
            }
            if (f) {
                if (sb.length() > 0) {
                    if (num > res.length) {
                        // extend array
                        String[] tmp = new String[res.length + res.length / 2];
                        System.arraycopy(res, 0, tmp, 0, num);
                        res = tmp;
                    }
                    res[num++] = sb.toString();
                }
                sb = new StringBuilder();
            } else {
                sb.append(c);
            }
        }
        if (sb.length() > 0) {
            if (num > res.length) {
                // extend array
                String[] tmp = new String[res.length + res.length / 2];
                System.arraycopy(res, 0, tmp, 1, num);
                res = tmp;
            }
            res[num++] = sb.toString();
        }
        String[] ret = new String[num];
        System.arraycopy(res, 0, ret, 0, num);
        return ret;
    }

    public static String replaceWithProperty(String s, String src, String dest) {
        int len = s.length();
        int bf = -1, ef = -1;
        StringBuffer sb = new StringBuffer();
        char last = ' ';
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if (c == '{' && last == PROPERTY_PREFIX) {
                bf = i;
                ef = -1;
            } else if (c == '}') {
                if (bf >= 0) { // hit property
                    ef = i;
                    String key = sb.toString();
                    if (src.equalsIgnoreCase(key)) {
                        StringBuffer tb = new StringBuffer();
                        return tb.append(s.substring(0, bf - 1)).append(dest).append(s.substring(ef + 1)).toString();
                    }
                } else {
                    bf = -1;
                    ef = -1;
                }
            } else {
                if (bf >= 0) {
                    sb.append(c);
                }
            }
            last = c;
        }
        return s;
    }

    public static String replace(String inString, String oldPattern, String newPattern) {
        if (inString == null) {
            return null;
        }
        if (oldPattern == null || newPattern == null) {
            return inString;
        }

        StringBuffer sbuf = new StringBuffer();
        // output StringBuffer we'll build up
        int pos = 0; // our position in the old string
        int index = inString.indexOf(oldPattern);
        // the index of an occurrence we've found, or -1
        int patLen = oldPattern.length();
        while (index >= 0) {
            sbuf.append(inString.substring(pos, index));
            sbuf.append(newPattern);
            pos = index + patLen;
            index = inString.indexOf(oldPattern, pos);
        }
        sbuf.append(inString.substring(pos));

        // remember to append any characters to the right of a match
        return sbuf.toString();
    }

    public static int countOccurrencesOf(String str, String sub) {
        if (str == null || sub == null || str.length() == 0 || sub.length() == 0) {
            return 0;
        }
        int count = 0, pos = 0, idx = 0;
        while ((idx = str.indexOf(sub, pos)) != -1) {
            ++count;
            pos = idx + sub.length();
        }
        return count;
    }

    public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) {
        if (str == null) {
            return null;
        }
        StringTokenizer st = new StringTokenizer(str, delimiters);
        List<String> tokens = new ArrayList<String>();
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (trimTokens) {
                token = token.trim();
            }
            if (!ignoreEmptyTokens || token.length() > 0) {
                tokens.add(token);
            }
        }
        return (String[]) tokens.toArray(new String[tokens.size()]);
    }

    public static String getProperty(String src) {
        int len = src.length();
        int bf = -1;
        StringBuffer sb = new StringBuffer();
        char last = ' ';
        for (int i = 0; i < len; i++) {
            char c = src.charAt(i);
            if (c == '{' && last == PROPERTY_PREFIX) {
                bf = i;
            } else if (c == '}') {
                if (bf >= 0) {
                    String key = sb.toString();
                    return key;
                } else {
                    bf = -1;
                }
            } else {
                if (bf >= 0) {
                    sb.append(c);
                }
            }
            last = c;
        }
        return null;
    }
}
